//
//  ScrollAddImageView.m
//  TruckSelling
//
//  Created by enghou on 2017/9/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

#import "ScrollAddImageView.h"
#import "ImageCollectionViewCell.h"
#import "SoleAddCell.h"
#import <objc/runtime.h>
@interface ScrollAddImageView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView *collection;

@end

@implementation ScrollAddImageView

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(110, 110);
        layout.minimumInteritemSpacing = 15;
        layout.minimumLineSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collection = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.showsHorizontalScrollIndicator = NO;
        [_collection registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"ImageCollectionViewCell"];
        [_collection registerClass:[SoleAddCell class] forCellWithReuseIdentifier:@"SoleAddCell"];
        [self addSubview:_collection];
        self.source = [NSMutableArray array];
        self.deletedFiles = [NSMutableArray array];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame maxNum:(NSInteger)maxNum{
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(110, 110);
        layout.minimumInteritemSpacing = 15;
        layout.minimumLineSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collection = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.showsHorizontalScrollIndicator = NO;
        [_collection registerClass:[ImageCollectionViewCell class] forCellWithReuseIdentifier:@"ImageCollectionViewCell"];
        [_collection registerClass:[SoleAddCell class] forCellWithReuseIdentifier:@"SoleAddCell"];
        [self addSubview:_collection];
        self.source = [NSMutableArray array];
        self.deletedFiles = [NSMutableArray array];
    }
    return self;
}

-(UIImageView *)imageViewForIndex:(NSInteger)index{
    ImageCollectionViewCell *cell = (ImageCollectionViewCell *)[_collection cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    return cell.imageView;
}

#pragma mark - CollectionViewDelegate And CollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_source count] + 1;
}

-(__kindof UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row >= [_source count]) {
        return [_collection dequeueReusableCellWithReuseIdentifier:@"SoleAddCell" forIndexPath:indexPath];
    }else{
        ImageCollectionViewCell *cell = [_collection dequeueReusableCellWithReuseIdentifier:@"ImageCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row != [_source count]) {
        if ([cell respondsToSelector:@selector(setModel:)]) {
            [cell performSelector:@selector(setModel:) withObject:self.source[indexPath.row]];
        }
        if ([cell respondsToSelector:@selector(setDel:)]) {
            __weak typeof(self) sself = self;
            __weak UICollectionView *wCollect = collectionView;
            [cell performSelector:@selector(setDel:) withObject:^(MediaItemModel *image){
                NSInteger index = [sself.source indexOfObject:image];
                if (index>=0 && index <= [sself.source count] ) {
                    [sself.source removeObject:image];
                    [wCollect deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
                    [self.deletedFiles addObject:image];
                }
            }];
        }
        return;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>=[_source count]) {
        if ([self.delegate respondsToSelector:@selector(showPicker)]) {
            [self.delegate showPicker];
            return;
        }
    }
    if ([self.delegate respondsToSelector:@selector(showBigImageWithCurrentIndex:imageSource:)]) {
        [self.delegate showBigImageWithCurrentIndex:indexPath.row imageSource:self.source];
    }
}

-(void)clearImages{
    if (self.source) {
        [self.source removeAllObjects];
    }
}

-(void)feedImage:(UIImage *)image{
    if (image != nil) {
        [self.source addObject:image];
        [_collection reloadData];
    }
}

-(void)feedImages:(NSArray *)images{
    if ([images count]>0) {
        [self.source addObjectsFromArray:images];
        [_collection reloadData];
    }
}

-(NSArray *)imageArray{
    NSMutableArray *imageArr = [NSMutableArray array];
    [self.source enumerateObjectsUsingBlock:^(UIImage *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *fileId = objc_getAssociatedObject(obj, @"fileId");
        if (!fileId) {
            [imageArr addObject:obj];
        }
    }];
    return imageArr;
}

-(void)dealloc{
    NSLog(@"View被释放了");
}

@end
